//
//  List_RootViewController.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "List_RootViewController.h"
#import "AddFruitTableViewCell.h"

@interface List_RootViewController ()

@property (nonatomic, strong) NSMutableArray *fruits;

@end

@implementation List_RootViewController

- (NSMutableArray *)fruits
{
    if (!_fruits) {
        _fruits = [NSMutableArray array];
    }
    return _fruits;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fruits = [NSMutableArray arrayWithArray:[LocalData getAllAddFruits]];
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"清单";
    
    [self.view addSubview:self.tableView];
    
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = 100;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AddFruitTableViewCell" bundle:nil] forCellReuseIdentifier:@"AddFruitTableViewCell"];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    self.tableView.frame = CGRectMake(0, navigationBar.frame.origin.y, SL_WINDOW_WIDTH, SL_WINDOW_HEIGHT - navigationBar.frame.origin.y - SL_VC_TabBar_HEIGHT);
}

# pragma UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.fruits.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddFruitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddFruitTableViewCell" forIndexPath:indexPath];
    cell.model = self.fruits[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FruitModel *model = self.fruits[indexPath.row];
    VHWeakSelf
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示"                                                                                     message:@"若已完成清单请选择完成，若不想要了请选择删除" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *finishAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       VHStrongSelf
        [self finishAction:model];
    }];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       VHStrongSelf
        [self deleteAction:model];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];

    [alertController addAction:finishAction];
    [alertController addAction:deleteAction];
    [alertController addAction:cancelAction];

    UIViewController *vc = [UIWindow currentViewController];
    [vc presentViewController:alertController animated:YES completion:nil];
}

- (void)finishAction:(FruitModel *)model
{
    model.finishTime = [NSString getNowTimeTimestamp];
    [LocalData saveFinishFruitInfoModel:model];
    
    [self deleteAddFruit:model];
    
    [kAppWindow makeToast:@"已完成！" duration:2 position:CSToastPositionTop];
}
- (void)deleteAction:(FruitModel *)model
{
    [self deleteAddFruit:model];
    [kAppWindow makeToast:@"已删除！" duration:2 position:CSToastPositionTop];
}

- (void)deleteAddFruit:(FruitModel *)model
{
    [LocalData deleteAddFruit:model.vege_id];
    self.fruits = [NSMutableArray arrayWithArray:[LocalData getAllAddFruits]];
    [self.tableView reloadData];
}


@end
